package com.bkgy.modularbx.coil_track_stats;

import cn.hutool.core.util.StrUtil;
import com.bkgy.modularbx.coil_track_stats.dto.CoilCountStatsDTO;
import com.bkgy.modularbx.coil_track_stats.dto.CoilTrackStatsDTO;
import com.bkgy.modularbx.coil_track_stats.service.*;
import com.bkgy.modularbx.coil_track_stats.vo.TrackStatsQueryVO;
import com.bkgy.sys.common.entity.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 分卷全流程各个产线统计数据
 *
 * @author dell
 * @date 2025-10-25
 */
@Tag(name = "分卷各个产线统计管理", description = "分卷各个产线统计管理")
@RestController
@RequestMapping("/coil_stats")
@Validated
public class TrackStatsController {

    @Resource
    private TrackingStats2280Service trackingStats2280Service;

    @Resource
    private TrackingStats2150Service trackingStats2150Service;

    @Resource
    private TrackingStats1630Service trackingStats1630Service;

    @Resource
    private TrackingStats1870Service trackingStats1870Service;

    @Resource
    private TrackingStatsRcl1630Service trackingStatsRcl1630Service;

    @Resource
    private TrackingStatsRcl1870Service trackingStatsRcl1870Service;

    @Resource
    private TrackingStatsRcl2150Service trackingStatsRcl2150Service;

    @Resource
    private CoilCountDailyService coilCountDailyService;

    @Operation(summary = "分卷统计各产线数据", description = "根据日期类型、日期范围和产线类型统计各产线数据")
    @ApiResponse(content = @Content(mediaType = "application/json",
        schema = @Schema(implementation = CoilTrackStatsDTO.class)))
    @PostMapping("/track")
    public ResultData<CoilTrackStatsDTO> getTrackStats(
        @Parameter(description = "查询参数") @RequestBody @Valid TrackStatsQueryVO queryVO) {

        // 根据产线类型调用相应的服务
        String lineType = queryVO.getLineType();
        CoilTrackStatsDTO statsResult;

        switch (lineType) {
            case "CAL2150":
                statsResult = trackingStats2150Service.getStatsByDateRange(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "CAL1630":
                statsResult = trackingStats1630Service.getStatsByDateRange(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "CGL1870":
                statsResult = trackingStats1870Service.getStatsByDateRange(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "RCL1630":
                statsResult = trackingStatsRcl1630Service.getStatsByDateRange(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "RCL1870":
                statsResult = trackingStatsRcl1870Service.getStatsByDateRange(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "RCL2150":
                statsResult = trackingStatsRcl2150Service.getStatsByDateRange(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            default:
                return ResultData.ResultDataFaultSelf("不支持的产线类型: " + lineType, null);
        }

        return ResultData.ResultDataSuccess(statsResult);
    }

    @Operation(summary = "获取分卷数统计数据", description = "根据日期类型和日期范围统计分卷数数据")
    @ApiResponse(content = @Content(mediaType = "application/json",
        schema = @Schema(implementation = CoilCountStatsDTO.class)))
    @PostMapping("/coilCount")
    public ResultData<CoilCountStatsDTO> getCountStats(
        @Parameter(description = "查询参数")
        @RequestBody @Valid TrackStatsQueryVO queryVO) {
        
        CoilCountStatsDTO statsResult = coilCountDailyService.getCountStats(queryVO);
        return ResultData.ResultDataSuccess(statsResult);
    }

    @Operation(summary = "获取产线统计概括", description = "获取产线统计信息的概括描述")
    @ApiResponse(content = @Content(mediaType = "application/json",schema = @Schema(implementation = String.class)))
    @PostMapping("/summary")
    public ResultData<String> getStatSummary(
        @Parameter(description = "查询参数")
        @RequestBody @Valid TrackStatsQueryVO queryVO) {

        // 根据产线类型调用相应的服务
        String lineType = queryVO.getLineType();
        String statsResult = StrUtil.EMPTY;

        switch (lineType) {
            case "CAL2150":
                statsResult = trackingStats2150Service.getCoilStatInfos(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "CAL1630":
                statsResult = trackingStats1630Service.getCoilStatInfos(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "CGL1870":
                statsResult = trackingStats1870Service.getCoilStatInfos(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "RCL2150":
                statsResult = trackingStatsRcl2150Service.getCoilStatInfos(
                        queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "RCL1630":
                statsResult = trackingStatsRcl1630Service.getCoilStatInfos(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            case "RCL1870":
                statsResult = trackingStatsRcl1870Service.getCoilStatInfos(
                    queryVO.getDateType(), queryVO.getStartDate(), queryVO.getEndDate());
                break;
            default:
                return ResultData.ResultDataFaultSelf("暂不支持该产线的统计概括: " + lineType, null);
        }

        return ResultData.ResultDataSuccess(statsResult);
    }

}